//
//  RaidersViewController.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/8/24.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "RaidersViewController.h"
#import "RaidersTableCell.h"
#import "RaiderDetailViewController.h"
@interface RaidersViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView * tableView;

@property (nonatomic, strong)NSMutableArray * dataSource;

@property (nonatomic, strong)UIImageView * imageView;

@property (nonatomic)NSInteger pageno;
@end

@implementation RaidersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupBg];
    [self setup];
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self.tableView.mj_header beginRefreshing];
}

- (void)setupBg {
    
    self.barBGAlpha = 0;
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _imageView.image = self.Bgimage;
    _imageView.backgroundColor = kThemeColor;
    [self.view addSubview:_imageView];
}

- (void)setup {
    
    // ios 11  scrollvew 的位置调整
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior =UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(20,kNavigationBarHeight, SCREEN_WIDTH - 40, SCREEN_HEIGHT - kNavigationBarHeight) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.rowHeight = (SCREEN_WIDTH - 40) * 9/16 + 55;
    _tableView.sectionFooterHeight = 0.01;
    
    _tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"RaidersTableCell" bundle:nil] forCellReuseIdentifier:@"RaidersTableCell"];
    self.title = MYLocalizedString(@"HOMEPAGE_STRATEGY", nil);
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    @weakify(self);
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self);
            _pageno = 0;
            [self loadDataSource];
            [self.tableView.mj_footer resetNoMoreData];
        }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        [self loadDataSource];
    }];

}

- (void)loadDataSource {
    
      @weakify(self);
    
      [NetAPIManager getRaidersBy:self.currentCityID start:@(self.pageno) count:@(9) block:^(NSArray *raidersArray, NSError *error)  {
          
          @strongify(self);
          [ActivityIndicatorView hideHUDAddedTo:self.view];
          
          
          if (self.pageno == 0) {
              self.dataSource =  [NSMutableArray arrayWithArray:raidersArray];
              [self.tableView.mj_header endRefreshing];
              if (self.dataSource.count < 3) {
                  
                  self.tableView.mj_footer.hidden = YES;
              } else {
                  self.tableView.mj_footer.hidden = NO;
              }
          }else {
              [self.dataSource addObjectsFromArray:raidersArray];
              [self.tableView.mj_footer endRefreshing];
          }
          
          if (raidersArray.count < 9) {
              [self.tableView.mj_footer endRefreshingWithNoMoreData];
          }
          
          [self.tableView reloadData];
          self.pageno ++;

      }];

    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RaidersTableCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RaidersTableCell"];
    
    Raiders * model = self.dataSource[indexPath.section];
    
    [cell configData:model];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UITableViewHeaderFooterView * headerView = [tableView  dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    
    if (!headerView) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"header"];
        headerView.backgroundColor = [UIColor clearColor];
    }
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 0.01;
    }
    
    return 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RaiderDetailViewController * detailVC = [[RaiderDetailViewController alloc] init];
    
    Raiders * raider = self.dataSource[indexPath.section];
    raider.seetimes = @(raider.seetimes.integerValue + 1).stringValue;
    detailVC.raider = raider;
    [self.navigationController pushViewController:detailVC animated:YES];
    
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
@end
